package com.webproject.action.equipmentManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import com.webproject.core.BaseAction;
import com.webproject.core.Page;
import com.webproject.entity.equipmentManager.Equipment;
import com.webproject.entity.equipmentManager.EquipmentCheckup;
import com.webproject.entity.equipmentManager.EquipmentType;
import com.webproject.service.equipmentManager.EquipmentManagerService;

public class EquipmentManagerAction extends BaseAction {
	private Page mpage;
	private EquipmentManagerService equipmentManagerService;
	private List<EquipmentType> ets;
	public List<EquipmentType> getEts() {
		return ets;
	}

	public void setEts(List<EquipmentType> ets) {
		this.ets = ets;
	}

	public Page getMpage() {
		return mpage;
	}

	public void setEquipmentManagerService(
			EquipmentManagerService equipmentManagerService) {
		this.equipmentManagerService = equipmentManagerService;
	}

	public void setMpage(Page mpage) {
		this.mpage = mpage;
	}
	public String queryEquipment(){
		log.info("-------------queryEquipment");
		mpage=getPage();
		equipmentManagerService.queryEquipment(mpage);
		setTotal(mpage);
		return SUCCESS;
	}
	
	public String editEquipment(){
		Equipment equipment=getEquipment();
		if (getOper().equals("add")) {
			equipmentManagerService.saveEquipment(equipment);
		} else if (getOper().equals("edit")) {
			equipmentManagerService.updateEquipment(equipment);
		} else if (getOper().equals("del")) {
		}
		return SUCCESS;
	}
	private Equipment getEquipment(){
		Equipment e=new Equipment();
		String id=getRequest().getParameter("id");
		String code=getRequest().getParameter("code");
		String type=getRequest().getParameter("type");
		String name=getRequest().getParameter("name");
		String manufacturer=getRequest().getParameter("manufacturer");
		String phone=getRequest().getParameter("phone");
		String linkman=getRequest().getParameter("linkman");
		String money=getRequest().getParameter("money");
		String buyTime=getRequest().getParameter("buyTime");
		String number=getRequest().getParameter("number");
		if(isNotEmpty(id)){
			e.setId(Integer.parseInt(id));
		}
		e.setCode(code);
		e.setType(type);
		e.setName(name);
		e.setManufacturer(manufacturer);
		e.setPhone(phone);
		e.setLinkman(linkman);
		e.setMoney(Float.parseFloat(money));
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			e.setBuyTime(sdf.parse(buyTime));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		e.setNumber(Integer.parseInt(number));
		return e;
	}
	
	public String queryEquipmentCheckup(){
		mpage=getPage();
		String equipmentId=getRequest().getParameter("equipmentId");
		mpage.setCondition(equipmentId);
		equipmentManagerService.queryEquipmentCheckup(mpage);
		setTotal(mpage);
		return SUCCESS;
	}
	
	public String editEquipmentCheckup(){
		EquipmentCheckup ec=getEquipmentCheckup();
		if (getOper().equals("add")) {
			equipmentManagerService.saveEquipmentCheckup(ec);
		} else if (getOper().equals("edit")) {
			equipmentManagerService.updateEquipmentCheckup(ec);
		} else if (getOper().equals("del")) {
			equipmentManagerService.deleteEquipmentCheckup(ec);
		}
		return SUCCESS;
	}
	
	private EquipmentCheckup getEquipmentCheckup(){
		EquipmentCheckup ec=new EquipmentCheckup();
		String id=getRequest().getParameter("id");
		String equipmentId=getRequest().getParameter("equipmentId");
		String checkupTime=getRequest().getParameter("checkupTime");
		String status=getRequest().getParameter("status");
		String result=getRequest().getParameter("result");
		String checkupMan=getRequest().getParameter("checkupMan");
		if(isNotEmpty(id)){
			ec.setId(Integer.parseInt(id));
		}
		ec.setEquipmentId(equipmentId);
		ec.setCheckupMan(checkupMan);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			ec.setCheckupTime(sdf.parse(checkupTime));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ec.setResult(result);
		ec.setStatus(status);
		return ec;
	}
	
	public String queryEquipmentType(){
		mpage=getPage();
		equipmentManagerService.queryEquipmentType(mpage);
		setTotal(mpage);
		return SUCCESS;
	}
	
	
	public String queryEquipmentTypes(){
		mpage=getPage();
		mpage.setRow(1000);
		equipmentManagerService.queryEquipmentType(mpage);
		ets=(List<EquipmentType>) mpage.getRows();
		return SUCCESS;
	}
	public String editEquipmentType(){
		EquipmentType type=getEquipmentType();
		if (getOper().equals("add")) {
			equipmentManagerService.saveEquipmentType(type);
		} else if (getOper().equals("edit")) {
			equipmentManagerService.updateEquipmentType(type);
		} else if (getOper().equals("del")) {
			equipmentManagerService.deleteEquipmentType(type);
		}
		return SUCCESS;
	}
	
	private EquipmentType getEquipmentType(){
		String id=getRequest().getParameter("id");
		String typeName=getRequest().getParameter("typeName");
		EquipmentType et=new EquipmentType();
		if(isNotEmpty(id)){
			et.setId(Integer.parseInt(id));
		}
		if(isNotEmpty(typeName)){
			et.setTypeName(typeName);
		}
		return et;
	}
}
